#!/usr/bin/env python

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import os
import datetime
import sys

def new_report(test_report): 
    lists = os.listdir(test_report)
    #列出目录的下所有文件和文件夹保存到lists 
    lists.sort(key=lambda fn:os.path.getmtime(test_report + "/" + fn))
    # #按时间排序 
    if "tmp" in lists[-1]:
        file_new = os.path.join(test_report,lists[-2])
    else:
        file_new = os.path.join(test_report,lists[-1])
    # #获取最新的文件保存到
    return file_new

def getopts():
    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument('zipfile', type=str, help='location of duplicate_file.csv.zip')
    return parser.parse_args()


def make_sure_path_exists(path):
    import errno
    try:
        os.makedirs(path)
    except OSError as e:
        if e.errno != errno.EEXIST:
            raise e

def is_chinese(uchar):
        """判断一个unicode是否是汉字"""
        if ord(uchar) >= 0x4e00 and ord(uchar) <= 0x9fa5:
            return True
        else:
            return False

def count_chinese_char_num(file_name):
    num = 0
    for item in file_name:
        if is_chinese(item):
            num = num + 1
    return num

def main():
    import codecs

    fp_chinese = codecs.open("dupes_chinese.txt", mode="w", encoding='utf-8')
    fp_non_chinese = codecs.open("dupes_non_chinese.txt", mode="w", encoding='utf-8')
    with codecs.open("dupes.txt", encoding='utf-8') as fp:
        lines = fp.readlines()
        line_buffer = []
        reorder_line_buffer = []
        reorder_line_buffer_no_chinese_char = []
        max_idx = 0
        chin_cha_max = 0
        for line_num,line in enumerate(lines):
            if line[0:2] == "# ":
                fp_chinese.write(line)
                fp_non_chinese.write(line)
            else:
                if line[0:4] == "    " or line_num==7:
                    line_buffer.append(line)
                if (line[0:4] != "    " or line_num == len(lines) - 1) and (line_num!=7):
                    for idx, item in enumerate(line_buffer[1:]):
                        if count_chinese_char_num(item.split("/")[-1]) > chin_cha_max:
                            chin_cha_max = count_chinese_char_num(item.split("/")[-1])
                            max_idx = idx
                    if chin_cha_max == 0:
                        reorder_line_buffer_no_chinese_char = line_buffer
                        for item in reorder_line_buffer_no_chinese_char:
                            fp_non_chinese.write(item)
                    else:
                        reorder_line_buffer.append(line_buffer[0])
                        reorder_line_buffer.append(line_buffer[max_idx+1])
                        for idx, item in enumerate(line_buffer[1:]):
                            if idx != max_idx:
                                reorder_line_buffer.append(item)
                        for item in reorder_line_buffer:
                            fp_chinese.write(item)
                    
                    # clear status
                    line_buffer = []
                    reorder_line_buffer = []
                    reorder_line_buffer_no_chinese_char = []
                    max_idx = 0
                    chin_cha_max = 0
                    line_buffer.append(line)

if __name__ == '__main__':
    main()
